<?php

namespace App\Workers;

use Workerman\Worker;
use App\Services\YikaoSpecialScoreSpiderService;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\ErrorLogHandler;

class YikaoSpecialScoreSpiderWorker
{
    private $logger;
    private $spiderService;

    public function __construct()
    {
        // 初始化日志
        $this->logger = new Logger('yikao_special_score_worker');
        $this->logger->pushHandler(new StreamHandler(storage_path('logs/yikao_special_score_worker.log'), Logger::DEBUG));
        $this->logger->pushHandler(new ErrorLogHandler());

        // 初始化采集服务
        $this->spiderService = new YikaoSpecialScoreSpiderService();
    }

    public function onWorkerStart(Worker $worker)
    {
        $this->logger->info("Worker启动，ID: {$worker->id}");

        try {
            $this->spiderService->crawlAllColleges();
            $this->logger->info("采集任务完成");
        } catch (\Exception $e) {
            $this->logger->error("采集任务失败: " . $e->getMessage());
        }
    }

    public function onWorkerStop(Worker $worker)
    {
        $this->logger->info("Worker停止，ID: {$worker->id}");
    }

    public function onWorkerReload(Worker $worker)
    {
        $this->logger->info("Worker重载，ID: {$worker->id}");
    }
}
